package org.jeecg.common.api.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.ErrorMsg;

import java.io.Serializable;

/**
 *   接口返回数据格式
 */
@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class ForeignResult<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 返回处理消息
	 */
	@ApiModelProperty(value = "返回处理消息")
	private String message = "成功";

	/**
	 * 返回代码
	 */
	@ApiModelProperty(value = "返回代码")
	private Integer status;

	public ForeignResult() {
	}

	public static <T> ForeignResult<T> error(String msg) {
		return error(0, msg);
	}
	
	public static <T> ForeignResult<T> error(int status, String msg) {
		ForeignResult<T> r = new ForeignResult<>();
		r.setStatus(status);
		r.setMessage(msg);
		return r;
	}

	public static <T> ForeignResult<T> ok(int status, String msg) {
		ForeignResult<T> r = new ForeignResult<>();
		r.setStatus(status);
		r.setMessage(msg);
		return r;
	}
	
	public static <T> ForeignResult<T> ok() {
		ForeignResult<T> r = new ForeignResult<>();
		r.setStatus(1);
		r.setMessage("成功");
		return r;
	}
	
	public static <T> ForeignResult<T> ok(String msg) {
		ForeignResult<T> r = new ForeignResult<>();
		r.setStatus(1);
		r.setMessage(msg);
		return r;
	}
	
	public static <T> ForeignResult<T> ok(T data) {
		ForeignResult<T> r = new ForeignResult<>();
		r.setStatus(1);
		return r;
	}

	public static <T> ForeignResult<T> ok(Integer status, String message) {
		ForeignResult<T> r = new ForeignResult<>();
		r.setMessage(message);
		r.setStatus(status);
		return r;
	}

	public static <T> ForeignResult<T> error(ErrorMsg errorMsg) {
		return error(errorMsg.getCode(), errorMsg.getMsg());
	}

	/**
	 * 无权限访问返回结果
	 */
	public static ForeignResult<Object> noauth(String msg) {
		return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
	}
}